//
//  LYLCommonFooterView.m
//  ONE
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LYLCommonFooterView.h"

@interface LYLCommonFooterView ()

/** 阴影 */
@property (nonatomic, strong) UIView *shadowView;

/** 数量label */
@property (nonatomic, strong) UILabel *countLabel;

/** 查看全部按钮 */
@property (nonatomic, strong) UIButton *allButton;

@end

@implementation LYLCommonFooterView
{
    // 底部视图类型
    LYLFooterViewType viewType;
    // 左侧文字
    NSString *leftFormatText;
    // 查看所有按钮
    NSString *allButtonTitle;
}

// 底部视图高
+ (CGFloat)footerViewHeight
{
    return 44;
}

// 阴影高
+ (CGFloat)footerViewHeightForShadow
{
    return 5;
}

- (instancetype)initWithFooterViewType:(LYLFooterViewType)type
{
    self = [super init];
    
    if (self) {
        viewType = type;
        [self createFooterView];
    }
    return self;
}

#pragma mark --创建底部视图
- (void)createFooterView
{
    if (viewType == LYLFooterViewTypeNone || _shadowView) {
        return;
    }
    
    self.backgroundColor = LYLViewControllerBGColor;
    self.clipsToBounds = YES;
    
    _shadowView = [UIView new];
    _shadowView.backgroundColor = [UIColor colorWithRed:0.99 green:0.99 blue:1.00 alpha:1.00];
    _shadowView.layer.shadowColor = LYLShadowColor.CGColor;
    _shadowView.layer.shadowOpacity = 0.3;   // 透明度
    _shadowView.layer.shadowRadius = 2;
    _shadowView.layer.shadowOffset = CGSizeZero;
    [self addSubview:_shadowView];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = LYLSeparatorColor;
    lineView.backgroundColor = [UIColor colorWithWhite:198 / 255.0 alpha:0.5];// #80C6C6C6
    [_shadowView addSubview:lineView];
    [lineView makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(0.5);
        make.left.bottom.right.equalTo(_shadowView);
    }];
    
    if (viewType == LYLFooterViewTypeShadow) {
        [_shadowView makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(2);
            make.left.top.right.equalTo(self).insets(UIEdgeInsetsMake(-1, 0, 0, 0));
        }];
        
        return;
    }
    else
    {
        [_shadowView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsMake(-3, 0, 3, 0));
        }];
    }
    
    switch (viewType) {
        case LYLFooterViewTypeNone:
            break;
        case LYLFooterViewTypeComment:
        {
            leftFormatText = @"共%ld条评论";
            allButtonTitle = @"全部评论";
            break;
        }
        case LYLFooterViewTypeMovieStory:
        {
            leftFormatText = @"共%ld个电影故事";
            allButtonTitle = @"全部电影故事";
            break;
        }
        case LYLFooterViewTypeMovieReview:
        {
            leftFormatText = @"共%ld条短评";
            allButtonTitle = @"全部短评";
            break;
        }
        case LYLFooterViewTypeShadow:
            break;
        default:
            break;
    }
    
    _countLabel = [UILabel new];
    _countLabel.textColor = LYLDarkGrayTextColor;
    _countLabel.font = FontWithSize(11);
    [self addSubview:_countLabel];
    [_countLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self).insets(UIEdgeInsetsMake(0, 12, 0, 0));
    }];
    
    [self configureViewWithCount:0];
    
    _allButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_allButton setTitle:allButtonTitle forState:UIControlStateNormal];
    [_allButton setTitleColor:LYLAppThemeColor forState:UIControlStateNormal];
    _allButton.titleLabel.font = FontWithSize(12);
    [_allButton addTarget:self action:@selector(didClickedAllButton) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_allButton];
    [_allButton makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(80, 40));
        make.centerY.equalTo(self);
        make.right.equalTo(self);
        make.left.greaterThanOrEqualTo(_countLabel.right).offset(8);
    }];
}

#pragma mark --查看全部按钮点击方法
- (void)didClickedAllButton
{
    if (_showAllItems) {
        _showAllItems();
    }
}

#pragma mark --设置底部视图
- (void)configureViewWithCount:(NSInteger)count
{
    if (viewType > LYLFooterViewTypeNone && viewType < LYLFooterViewTypeShadow) {
        _countLabel.text = [NSString stringWithFormat:leftFormatText, count];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
